package com.wens.ui.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import kotlin.math.min

class RoundImageView :
    androidx.appcompat.widget.AppCompatImageView {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private val paint = Paint()


    private val roundPath:Path = Path()

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        roundPath.reset()
        roundPath.addCircle(measuredWidth/2f,measuredHeight/2f, min(measuredWidth,measuredHeight)/2f,Path.Direction.CCW)
    }

    override fun onDraw(canvas: Canvas?) {
        canvas?.save()
        canvas?.clipPath(roundPath)
        super.onDraw(canvas)
        canvas?.restore()
    }
}